/*
 * httpcachingresolver.h
 *
 *  Created on: Oct 8, 2011
 *      Author: anonovermind <anonovermind@gmail.com>
 */

#ifndef HTTPCACHINGRESOLVER_H_
#define HTTPCACHINGRESOLVER_H_

#include <string>
#include <list>
#include <map>
#include <boost/function.hpp>
#include <boost/asio.hpp>

namespace Wishmaster2
{
namespace Network
{

class HttpCachingResolver
{
public:
	HttpCachingResolver(boost::asio::io_service* service);
	virtual ~HttpCachingResolver();

	typedef boost::function<void (const boost::system::error_code&, boost::asio::ip::tcp::resolver::iterator)> resolveResultFunction;

	void resolve(const std::string& host, resolveResultFunction resultCallback);

protected:
	virtual void doQuery(const std::string& host);

	void resolveResult(const std::string& host, const boost::system::error_code& error,
			boost::asio::ip::tcp::resolver::iterator endpoint_iterator);

private:

	struct Query
	{
		std::string host;
		resolveResultFunction callback;
	};

	std::list<Query> m_queries;
	std::map<std::string, boost::asio::ip::tcp::resolver::iterator> m_finished;
	boost::asio::io_service* m_service;
	boost::asio::ip::tcp::resolver* m_resolver;
};

} /* namespace Network */
} /* namespace Wishmaster2 */
#endif /* HTTPCACHINGRESOLVER_H_ */
